const jwt = require('jsonwebtoken')

module.exports = () => {
    return async (ctx,next) => {
        const { app, socket, logger, helper } = ctx;
        const client = socket.id;
        const nsp = app.io.of('/');
        const query = socket.handshake.query;
        const { token } = query
        if (!token) {
            socket.emit('notify', helper.parseMsg('token','您未登录！'))
            socket.disconnect(true)
            return
        }
        let { str = '' } = await jwt.verify(token, 'Ftc_token_encode_str')
        let res = await ctx.model.User.findOne({ user_id: str, token })
            socket.user = res
        if (res&&res.id) {
            socket.emit('setUser', helper.parseMsg('user',res))
        } else {
            socket.emit('notify', helper.parseMsg('token','您未登录！'))
            socket.disconnect(true)
            return
        }
        await next()
    }
}